package com.daka.controller;

import com.daka.annotation.Login;
import com.daka.entity.Version;
import com.daka.enumerate.CommonStatus;
import com.daka.enumerate.DictionaryMap;
import com.daka.enumerate.StatusCode;
import com.daka.service.AppVersionService;
import com.daka.tools.CheckLogin;
import com.daka.vo.response.DataResponseVo;
import com.daka.vo.response.ListDataResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * @author beliveli on 2016/3/31 0031.
 */
@Controller
@RequestMapping("/version")
@Slf4j
public class AppVersionController {

    @Resource
    private AppVersionService appVersionService;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/list", consumes = "application/json")
    @Login
    public ListDataResponseVo<Version> list(HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.APP.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new ListDataResponseVo<Version>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }


        List<Version> versionList = appVersionService.selectAll();
        return new ListDataResponseVo<Version>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), versionList);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/save", consumes = "application/json")
    @Login
    public DataResponseVo<Version> save(@Valid @RequestBody Version version, HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.APP.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new DataResponseVo<Version>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }

        Version result = appVersionService.save(version);

        if (result == null) {
            return new DataResponseVo<Version>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<Version>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/updateStatus", consumes = "application/json")
    @Login
    public DataResponseVo<Version> updateStatus(@Valid @RequestBody Version version, HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.APP.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new DataResponseVo<Version>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }

        Version result = appVersionService.selectById(version.getVersionId());

        if (result == null) {
            return new DataResponseVo<Version>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        int resultCount;
        if (result.getStatus() == CommonStatus.NORMAL.getStatusCode()) {
            result.setStatus(CommonStatus.DISABLED.getStatusCode());
            resultCount = appVersionService.updateStatus(CommonStatus.DISABLED.getStatusCode(), version.getVersionId());
        } else {
            result.setStatus(CommonStatus.NORMAL.getStatusCode());
            resultCount = appVersionService.updateStatus(CommonStatus.NORMAL.getStatusCode(), version.getVersionId());
        }

        if (resultCount == 0) {
            return new DataResponseVo<Version>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<Version>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }
}
